<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<script type="text/javascript" src="../js/prototype.js"></script>
<%@page import="java.util.List"%>
<%@page import="com.solution.model.QuestionType"%>
<%@page import="com.solution.model.AnswerType"%>
<%@page import="com.solution.constant.LoginConstants"%>
<%@page import="java.util.HashMap"%>
<link href="../css/style.css" rel="stylesheet" type="text/css" />

<% List<QuestionType> questionTypeList= (List<QuestionType>)session.getAttribute(LoginConstants.EXAMINEE_QUESTIONS); 
Integer index = (Integer)session.getAttribute(LoginConstants.EXAMINEE_QUESTION_INDEX);
QuestionType questionType = questionTypeList.get(index);
HashMap<Long,Long> queAnswer = (HashMap<Long,Long>)session.getAttribute(LoginConstants.EXAMINEE_QUESTION_ANSWER);
%>
<script language="javascript">
	<bean:define id="totalSec" name="examineeForm" property="examType.time" />
	<%long min =((Long)totalSec)/60;
	long sec=((Long)totalSec)%60;%>
	
	var sec = <%=sec%>;   // set the seconds
	var min = <%=min%>;// set the minutes
	

	function countDown() {
	   sec--;
	  if (sec == -01) {
	   sec = 59;
	   min = min - 1; }
	  else {
	   min = min; }

	if (sec<=9) { sec = "0" + sec; }

	  time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";

	if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }

	SD=window.setTimeout("countDown();", 1000);
	if (min == '00' && sec == '00') { 
		sec = "00";
		window.clearTimeout(SD);	
		window.close();
	 }
	}
	window.onload = countDown;

	function nextQuestion()
	{	var ans = document.getElementsByName("answerId");
		var answer = -1;
		if(ans!= null){
			for(i=0;i<ans.length;i++){			
				if(ans[i].checked){
					answer=ans[i].value;
				}
			}
		}
		new Ajax.Request('/showAjaxQuestion.do?nextOrPrevious=next&answerId='+answer,   
			{     method:'get',     
				  onSuccess: function(transport)
					{       
						var response = transport.responseText || "no response text";
						$('questionArea').innerHTML = response;
					},     
				  onFailure: function()
				    { 
						alert('Something went wrong...') 
					}   
			}
		); 
	}
	function prevQuestion()
	{
		var ans = document.getElementsByName("answerId");
		var answer = -1;
		if(ans!= null){
			for(i=0;i<ans.length;i++){			
				if(ans[i].checked){
					answer=ans[i].value;
				}
			}	
		}
		new Ajax.Request('/showAjaxQuestion.do?nextOrPrevious=previous&answerId='+answer,   
			{     method:'get',     
				  onSuccess: function(transport)
					{       
						var response = transport.responseText || "no response text"; 
						$('questionArea').innerHTML = response;		
						
						
					},     
				  onFailure: function()
				    { 
						alert('Something went wrong...') 
					}   
			}
		); 
	}
	function resetQuestion(){
		var ans = document.getElementsByName("answerId");		
		if(ans!= null){
			for(i=0;i<ans.length;i++){
				ans[i].checked = false;					
			}	
		}	
	}
	</script>

<html:form action="/showQuestion">
<table width="650px" border="0" align="center" cellpadding="0" cellspacing="0" class="tbletext">
<tr>
		<td colspan="3">
	<table><tr>
	<td>
	<input type="RADIO" onBlur="countTemp();" id="temp" checked>
	</td>
	<td colspan="2">
	<div id="theTime" class="timeClass"></div>
	<td>
	</tr>
	</table>
	</td>
						
				</tr>
				
<tr><td>
		<div id="questionArea">
		<table width="650px" border="0" align="center" cellpadding="0" cellspacing="0" class="tbletext">
		<tr>
			<td  align="right" colspan="3"><input type="button" class="imageButton" value="Finish" onClick="finishExamPopup();"></td>						
		</tr>
		<tr>
			<td colspan="3">
			<table>
			<tr>
				<td >Total Question: <b><%=questionTypeList.size()%></b></td>
			</tr>
			<tr>
				<td >Attempted: <b><%=queAnswer.size()%></b></td>
			</tr>
			<tr>
				<td >Attempting: <b><%=(index+1)%></b> of<b> <%=questionTypeList.size()%></b> </td>
			</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td colspan="3"></td>
		</tr>
		<tr>
			<td colspan="3" valign="top"  bgcolor="#CCCCCC"><img src="images/zero.gif" width="1" height="1" /></td>
		</tr>
		<tr>
			<td colspan="3"></td>
		</tr>
		<tr>
			<td colspan="3">
			<table>
					<tr>
						<td  style="width:20px;">Q<%=(index+1)%>.</td>
						<td align="left" valign="top" colspan="2"><%=questionType.getQuestionStatement() %></td>			
						
					</tr>
					
					<tr>
						<td style="width:20px;"></td>			
						<td align="left" valign="top" colspan="2">
						<%if(questionType.getImageType()!= null){ %>			
				
						<%String contextPath = session.getAttribute("contextPath").toString();%>
						<img src='<%=contextPath+"/image?imageType=large&image_id="%><%=questionType.getImageType().getId() %>'
						alt="No Image Present">
						<%} %>			
						</td>
					</tr>
					<logic:iterate name="examineeForm" property="answerTypeList" id="answerType">
					<tr >
						<bean:define id="aID" name="answerType" property="id"/>			
						<%if((Long)queAnswer.get(questionType.getId())!= null)	{%>
						<%if(aID.equals((Long)queAnswer.get(questionType.getId()))){%>
						<td style="width:20px;"><input type="radio" name="answerId" id="answerId" value="<%=String.valueOf(aID)%>" checked="checked"></td>			
						<%} else{ %>			
						<td style="width:20px;"><input type="radio" name="answerId" id="answerId" value="<%=String.valueOf(aID)%>" ></td>
						<%} %>
						<%} else{%>
						<td style="width:20px;"><input type="radio" name="answerId" id="answerId" value="<%=String.valueOf(aID)%>" ></td>
						
						<%} %>			
						
						<td align="left"><bean:write name="answerType" property="answerValue"/></td>
						<td align="left"><%if(((AnswerType)answerType).getImageType()!= null) {%>
						<%String contextPath = session.getAttribute("contextPath").toString();%>
						<img src='<%=contextPath+"/image?imageType=large&image_id="%><%=((AnswerType)answerType).getImageType().getId() %>'
						alt="No Image Present">
						<%}else{%>
						&nbsp;
						<%}%>
						</td>
							
						
						
					</tr>
					</logic:iterate>
					<tr>
						<td align="left" valign="top" colspan="3"></td>			
						
					</tr>
					<tr>
						<td style="width:20px;"></td>
						<td align="left" valign="top" colspan="2"><%if(index>0){%><input type="button" class="imageButton" value="Previous" onClick="javascript:prevQuestion()"><%}%>			
						
						<%if(index<questionTypeList.size()-1){%><input type="button" class="imageButton" value="Next" onClick="javascript:nextQuestion()"><%}%> <input type="button" class="imageButton" value="Reset" onClick="javascript:resetQuestion()"></td>			
					</tr>
					</table>
			</td>
		</tr>
    </table>
			</div>
		</td>
	</tr>

    </html:form>
	
	<script type="text/javascript">
var ii = 0;
var s;
var isStarted=0;
var balancer = 0;
function countTemp(){
	balancer--;
	
	if(isStarted==0){
		count();
		isStarted =1;
	}
}
function finishExamPopup()
	{
		nextQuestion();
		window.close();
	}
function count(){
ii++;
s=window.setTimeout('count()',1000);
if(ii>40){
	//alert("Time Over");
	window.clearTimeout(s);
	//finishExamPopup();
	
	if(navigator.appName == 'Netscape'){		
		window.close();
	}else{
		//var xWin=window.dialogArguments;
		//xWin.document.finishExam();
		
		//finishExamPopup();
		window.close();
	}
	//var xWin=window.dialogArguments;
	//xWin.document.finishExam();
	//window.close();
}
}
function clear(){
	window.clearTimeout(s);
	isStarted=0;
	balancer=0;	
	document.getElementById('temp').focus();
}
function clearSetFocus(){
	balancer++;	
	if(balancer==0){
		ii--;
	}else{
		balancer = 0;
	}
	window.clearTimeout(s);
	document.getElementById('temp').focus();
	isStarted = 0 ;
}
if(navigator.appName == 'Netscape'){
//window.document.body.setAttribute('onblur','count();');
window.document.body.setAttribute('onfocus','clear();');
//window.document.setAttribute('onclick','clearSetFocus();');
window.document.onclick= clearSetFocus;
}else{
//window.document.body.onblur= count;
window.document.body.onfocus= clear;
window.document.onclick= clearSetFocus;
}
document.getElementById('temp').focus();
</script>
